﻿/*
 * Copyright (C) 2012 TheLionsRoar <thelionwillalwaysroar@gmail.com>
 * Copyright (C) 2012 Jedrix <jedrix2454@gmail.com>
 * 
 * This file is part of the LEO Game Library.
 * 
 * LEO is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * LEO is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have receieved a copy of the GNU General Public License
 * along with LEO. If not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace LEO.Framework {
    public class ParticleEmitter : Drawable {
        public List<Texture2D> ParticleIcons;
        public int AmountOfParticles { get; set; }
        public int TimeToLiveMax { get; set; }
        public int LifeSpeedMax { get; set; }
        private List<Particle> particles;
        private Random random;

        public ParticleEmitter(Scene scene, int amountOfParticles, int timeToLiveMax, int lifeSpeedMax) {
            ParticleIcons = new List<Texture2D>();
            particles = new List<Particle>();
            random = new Random();
            AmountOfParticles = amountOfParticles;
            TimeToLiveMax = timeToLiveMax;
            LifeSpeedMax = lifeSpeedMax;
            Visible = true;
            Position = new Vector2();
        }
        public override void Update(GameTime gameTime) {
            for(int i = 0; i < AmountOfParticles; i++) {
                particles.Add(GenerateNewParticle());
            }
            for(int particle = 0; particle < particles.Count; particle++) {
                particles[particle].Update(gameTime);
                if(particles[particle].TimeToLive <= 0) {
                    particles[particle].Visible = false;
                    particles.RemoveAt(particle);
                    particle--;
                }

            }

            base.Update(gameTime);
        }

        public override void Draw(SpriteBatch spriteBatch) {
            for(int particle = 0; particle < particles.Count; particle++) {
                if(particles[particle].Visible) {
                    particles[particle].Draw(spriteBatch);
                }
            }
        }

        private Particle GenerateNewParticle() {
            Texture2D particleIcon = ParticleIcons[random.Next(ParticleIcons.Count)];
            Vector2 position = Position;
            Vector2 velocity = new Vector2((float)random.NextDouble() * 2 - 1, (float)random.NextDouble() * 2 - 1);
            Color color = new Color((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble());
            float size = (float)random.NextDouble();
            int timeToLive = random.Next(TimeToLiveMax);
            float lifeSpeed = random.Next(1, LifeSpeedMax);

            return new Particle(particleIcon, color, position, timeToLive, lifeSpeed, size, velocity);
        }
    }
}
